<template>
  <div class="login-container">
    <el-row>
      <el-col :span="6" :offset="9">

          <h3>奖学金系统</h3>

        <el-form :model="loginModel" size="large">
          <el-form-item>
            <el-input v-model="loginModel.username" placeholder="用户名"></el-input>
          </el-form-item>
          <el-form-item>
            <el-input type="password" v-model="loginModel.password" placeholder="密码"></el-input>
          </el-form-item>
          <el-form-item>
            <el-select v-model="loginModel.roleKey" placeholder="角色">
              <el-option value="STUDENT" label="学生"></el-option>
              <el-option value="MANAGER" label="管理员"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="onSubmit" style="width: 100%">登录</el-button>
          </el-form-item>
        </el-form>

      </el-col>
    </el-row>

  </div>
</template>

<script>
import request from '@/utils/http';
import { useRouter } from "vue-router";
import { ElMessage } from 'element-plus'
let router;
export default {
  name: 'Login',
  data() {
    return {
      loginModel: {
        username: 'lisi',
        password: '123456',
        roleKey: 'STUDENT'
      }
    }
  },
  setup() {
    router = useRouter()
  },
  methods: {
    onSubmit() {
      let url = "/manager/login"
      if (this.loginModel.roleKey === 'STUDENT') {
        url = "/student/login"
      }
      request.post(url, this.loginModel)
        .then(response => {
          if (response.code === 200) {
            let data = JSON.stringify(response.data)
            sessionStorage.setItem('login', data)
            router.push('/welcome')
          } else {
            ElMessage({
              message: response.msg,
              grouping: true,
              type: 'error',
            })
          }
        })
    }
  }

}
</script>

<style scoped>
.login-container {
  margin-top: 200px;

}
</style>
